/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cpal.entrenamiento.common.logger;

import org.apache.commons.validator.GenericValidator;

/**
 *
 * @author omar
 */
public class StringUtils {

    public static final String ES_LANGUAGE = "ES";
    public static final String EN_LANGUAGE = "EN";
    private static final String CLASSID_PATTERN =
            "^[A-Z0-9]{5}$";

    public static String trim(String param) {
        return null == param ? null : param.trim();
    }

    public static boolean isEmpty(String param) {
        return null != param || 0 != param.length() ? false : true;
    }

    public static String extractValue(String queryString, String key) {
        String string = null;
        try {
            if (queryString.indexOf(key) != -1) {
                string = queryString.substring(queryString.indexOf(key) + key.length());
                if (string.indexOf("&") != -1) {
                    string = string.substring(0, string.indexOf("&"));
                } else if (string.indexOf("</") != -1) {
                    string = string.substring(0, string.indexOf("</"));
                }
            }
        } catch (Exception ex) {
        }
        return string;
    }

    /**
     * Joining all messages
     *
     * @param messages messages to join
     * @return message after joined
     */
    public static String joinMessages(Object... messages) {
        StringBuilder sb = new StringBuilder();
        if (null != messages) {
            for (Object message : messages) {
                sb.append(message);
            }
        } else {
            return null;
        }
        return sb.toString();
    }

    /**
     * Validate CLASSID Format with regular expression
     *
     * @param ip ip address for validation
     * @return true valid ip address, false invalid ip address
     */
    public static boolean isClassId(String classId) {
        if (GenericValidator.matchRegexp(classId, CLASSID_PATTERN)) {
            return true;
        } else {
            return false;
        }
    }
}
